<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

class Block_Post_Search extends Core_Block {

	protected $_requestVar = 'q';
	
	protected $_collection = null;
	
	public function _beforeRender() {
		$collection = $this->getPostCollection();
		$search = App::get($this->_requestVar);
		if (!empty($search)) {
			$where = sprintf("(post.title LIKE '%%%s%%' OR post.content LIKE '%%%s%%')", $search, $search);
			$collection->getSelect()->where($where);
		}
	}
	
	protected function getPostCollection() {
		if (!$this->_collection) {
			if ($layer = $this->getLayout()->getBlock('post_layer'))
				$this->_collection = $layer->getCollection();
			else
				$this->_collection = App::object('Entity_Post')->getCollection();
		}
		return $this->_collection;
	}
}